//
//  RDAddBloodSugarViewController.m
//  ReverseDiabetes
//
//  Created by 王佩璇 on 2024/8/26.
//

#import "RDAddBloodSugarViewController.h"
#import "RDTimeTagCollectionViewCell.h"
#import "RDBloodSugar.h"
#import "RDBloodSugarTag.h"

#import "WSDatePickerView.h"

@interface RDAddBloodSugarViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *collectionViewFlowLayout;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UILabel *recordingTimeLab;
@property (weak, nonatomic) IBOutlet UITextField *bloodSugarValueTF;

@property (nonatomic, strong) NSMutableArray *tagArr;

@property (nonatomic, strong) RDBloodSugarTag *selectSugarTag;

@property (nonatomic, strong) RDBloodSugar *addBloodSugar;
@end

@implementation RDAddBloodSugarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"添加血糖记录";
    
    
    self.addBloodSugar = [[RDBloodSugar alloc]init];
    self.tagArr = [[RDBloodSugarTagDBManager sharedManager] getAllTag];
    
    [self setupUI];
}




- (IBAction)selectTimeClick:(id)sender {
    //年-月-日
    WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay CompleteBlock:^(NSDate *selectDate) {
        
        NSString *dateString = [selectDate stringWithFormat:@"yyyy-MM-dd"];
        self.recordingTimeLab.text = dateString;
        NSLog(@"选择的日期：%@",dateString);
      
    }];
    datepicker.dateLabelColor = kBlackColor;//年-月-日-时-分 颜色
    datepicker.datePickerColor = kBlackColor;//滚轮日期颜色
    datepicker.doneButtonColor = kMainColor;//确定按钮的颜色
    [datepicker show];
}



- (void)setupUI{
    NSDate *date = [NSDate date];
    NSString *nowTime = [date stringWithFormat:@"yyyy-MM-dd"];
    self.recordingTimeLab.text = nowTime;
    self.addBloodSugar.recording_time = nowTime;
    
    NSString *createTime = [date stringWithFormat:@"yyyy-MM-dd hh:mm:ss"];
    self.addBloodSugar.create_time = createTime;
    
    NSString *hourTime = [date stringWithFormat:@"hh"];
    NSString *minTime = [date stringWithFormat:@"mm"];
    self.selectSugarTag = [RDBloodSugarTool getTagByHour:@([hourTime integerValue]) andMinute:@([minTime integerValue])];
    
    
    self.collectionViewFlowLayout.minimumLineSpacing = 5;
    self.collectionViewFlowLayout.minimumInteritemSpacing = 5;
    self.collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorNamed:@"白色"];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"RDTimeTagCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"kRDTimeTagCollectionViewCellID"];

}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return CGSizeMake((SCREEN_WIDTH-30)/5.0, 35);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.tagArr.count;
}

- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    RDTimeTagCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kRDTimeTagCollectionViewCellID" forIndexPath:indexPath];
    
    RDBloodSugarTag *model = self.tagArr[indexPath.row];
    cell.model = model;
    
    if (!IsNilOrNull(self.selectSugarTag)) {
        [cell selectCell:(model.tag_id.intValue == self.selectSugarTag.tag_id.intValue)];
    }
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 
    self.selectSugarTag = self.tagArr[indexPath.row];
    
    [self.collectionView reloadData];
}



- (IBAction)addClick:(id)sender {
 
    if (self.bloodSugarValueTF.text.length == 0) {
//        SVProgressInfo(@"请输入血糖");
        [SVProgressHUD showInfoWithStatus:@"请输入血糖"];
        return;
    }

    
    self.addBloodSugar.blood_sugar_value = self.bloodSugarValueTF.text;
    self.addBloodSugar.blood_sugar_tag = self.selectSugarTag.tag_id;
    self.addBloodSugar.recording_time = self.recordingTimeLab.text;
    
    [[RDBloodSugarDBManager sharedManager] saveBloodSugar:self.addBloodSugar];
    
    [SVProgressHUD showSuccessWithStatus:@"添加成功"];
    [self.navigationController popViewControllerAnimated:YES];
}





@end
